import configparser
from src.tool import throw
import json
import src.constant as constant
from src.net import Server
from database.dbop import Database
import os
import src.global_config as gl
import src.tool as tool
from src.log import Log

# 配置文件地址
filename = './config.ini'


def bless():
    print(""
          " #     #                                      \n" +
          " #     #  ####  #    # # #    # #####   ####  \n" +
          " #     # #    # ##   # # ##   # #    # #    # \n" +
          " ####### #    # # #  # # # #  # #####  #    # \n" +
          " #     # #    # #  # # # #  # # #    # #    # \n" +
          " #     # #    # #   ## # #   ## #    # #    # \n" +
          " #     #  ####  #    # # #    # #####   ####  ")


# 主函数
if __name__ == '__main__':
    # 读取程序配置
    config = configparser.ConfigParser()
    config.read(filename, encoding='utf-8')
    port = config['tcp']['port']

    # 设置全局变量
    dbpath = os.path.dirname(os.path.realpath(__file__))
    gl.set_value('logpath', dbpath)

    # 一定要在设置完全局变量再实例化
    old = os.path.join(dbpath, tool.get_config_by_key(config['db'], 'old'))
    current = os.path.join(dbpath, tool.get_config_by_key(config['db'], 'current'))
    ai = os.path.join(dbpath, tool.get_config_by_key(config['db'], 'ai'))
    Tom = os.path.join(dbpath, tool.get_config_by_key(config['db'], 'Tom'))

    # 获取数据集
    model = {
        "old": old,
        'current': current,
        'ai': ai,
        "Tom": Tom
    }
    gl.set_value('model', model)

    # ai相关的存全局变量
    gl.set_value('ai', config['ai'])
    gl.set_value('model_path', config['model'])
    log = Log()

    # 没有端口就抛出错误
    if port is None:
        log.tocrit("缺少端口号")
        throw(format(constant.LACK_OF_PARAMS, 'port'))

    bless()
    net = Server()
    net.init()
    net.run(port)

